#!/usr/bin/python3
#-*-coding:utf-8-*- 
import hashlib
from flask import make_response, jsonify
import json
import os, base64
from init import aes
import datetime
import time


# md_5 
def md5(_str):
    _str = _str
    m = hashlib.md5(_str.encode('utf-8'))
    return m.hexdigest()
    pass

def md5_16(_str):
    _str = _str
    m = hashlib.md5(_str.encode('utf-8')).hexdigest()[8:-8]
    return m
    pass

# 返回josn  issuccess = 1 表示成功，0 标示失败
def response_json(object,issuccess=1,msg=""):
    dic_json = {
        "status": str(issuccess), # 1 成功， 0 异常
        "data":[],
        "msg":msg
    }
    if issuccess == 1 :
        dic_json["data"] = object

    return make_response(jsonify(dic_json))

# 返回josn  issuccess = 1 表示成功，0 标示失败
def response_aes_encrypt_json(object,issuccess=1,msg=""):
    dic_json = {
        "status": str(issuccess), # 1 成功， 0 异常
        "data":[],
        "msg":msg
    }
    if issuccess == 1 :
        dic_json["data"] = object
    # return make_response(jsonify(dic_json))
    return make_response(jsonify({'data':aes.encrypt(json.dumps(dic_json))}))


def get_user_tag(headers):
    user_tag = 0
    if "x-tag" in headers:
        user_tag = headers["x-tag"]
        pass
    return int(user_tag)


def get_identifier_id(headers):
    identifier_id = None
    if "identifier_id" in headers:
        identifier_id = headers["identifier_id"]
        pass
    return identifier_id

def get_use_day(headers):
    # use_day = 1
    # if "use_active_day" in headers:
    #     use_day = headers["use_active_day"]
    #     pass

    # 使用app时间
    use_day = get_duration_day(headers)
    app_version = get_app_version(headers)
    # 2019-07-04 21:39:00 第一周上线初始化设置为1，之后看版本情况变化
    if app_version <= 136:
        # starnd_date = datetime.datetime(2019,7,4,23,59,59)
        # now_date = datetime.datetime.now()
        # interval = now_date - starnd_date
        # interval_day = interval.days
        # if interval_day > 0:
        #     use_day = interval_day
        # else:
        #     use_day = 1
        use_day = 1
    
    return int(use_day)
    
def get_app_version(headers):
    version = 134
    if "User-Agent" in headers:
        user_agent = headers["User-Agent"]
        version_str = user_agent.split('(')[0].split('/')[1]
        version_str = version_str.replace('.',"")
        version = int(version_str)
    return version
    pass

def get_duration_day(headers):
    duration_day = 1
    if "duration_active_day" in headers:
        duration_day = headers["duration_active_day"]
        pass
    return int(duration_day)


def decode_base64(_str):
    decode = base64.b64decode(_str.encode('utf-8'))
    return str(decode, 'utf-8')

def encode_base64(_str):
    encode = base64.b64encode(_str.encode('utf-8'))
    return str(encode,'utf-8')

# 今日0点时间
def today_start():
    today = datetime.date.today() 
    today_time = int(time.mktime(today.timetuple())) 
    return str(today_time)
    pass

# 当前时间
def current_time_timestamp():
    current_time = int(time.time())
    return str(current_time)
    pass

# 当前时间 格式化 显示
def current_time_format():
    timestamp = int(time.time())
    formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
    return str(formatted_time) # 这个时间是根据服务器所在的时间显示的，服务器上的为 0 时区（UTC）。
    pass

# 昨日凌晨时间
def yesterday_start():
    today = datetime.date.today() 
    today_time = int(time.mktime(today.timetuple()))
    yesterday_start = today_time - 24 * 60 * 60
    return str(yesterday_start)
    pass

# 距离现在的n天的开始日期和结束日期
def time_start_end(offset_day, duration_day = 0):
    today = datetime.date.today() 
    today_time = int(time.mktime(today.timetuple()))
    start = today_time - offset_day * 24 * 60 * 60
    if duration_day == 0:
        end = today_time
    else:
        end = start + duration_day * 24 * 60 * 60
    
    return (start,end)
    pass


def parse_request_json(request_form):
    if "data" not in request_form:
        return {}
    
    data = request_form["data"]
    de_data = aes.decrypt(data)
    return json.loads(str(de_data))
    pass









    
